package email

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"vrcm/src/vrcm/app/admin/service/dto/email"
	emailService "vrcm/src/vrcm/app/admin/service/email"
	"vrcm/src/vrcm/common/apis"
)

type Email struct {
	apis.Api
}

// SendEmailCode 发送验证码
func (s Email) SendEmailCode(c *gin.Context) {
	req := email.SendEmailCodeReq{}
	email := emailService.EmailService{}
	err := s.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&email.Service).
		Errors
	if err != nil {
		s.Logger.Error(err)
		s.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = email.SendEmailCode(&req)
	if err != nil {
		s.Error(apis.SendEmailCodeError.Code, err, fmt.Sprintf("发送邮箱验证码，\r\n失败信息 %s", err.Error()))
		return
	}
	s.OK("", "发送邮箱验证码成功")
}

// CheckEmailCode 校验验证码
func (s Email) CheckEmailCode(c *gin.Context) {
	req := email.CheckEmailCodeReq{}
	email := emailService.EmailService{}
	err := s.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&email.Service).
		Errors
	if err != nil {
		s.Logger.Error(err)
		s.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = email.CheckEmailCode(&req)
	if err != nil {
		s.Error(apis.CheckEmailCodeError.Code, err, fmt.Sprintf(err.Error()))
		return
	}
	s.OK("", "校验邮箱验证码成功")
}
